import { queryCurrentUserInfo, postAuthentication } from '@/services/api';

export default {
  namespace: 'user',
  state: {
    info: {},
  },
  effects: {
    *getUserInfo(_, { call, put }) {
      const response = yield call(queryCurrentUserInfo);
      const { code, msg, data } = response;
      if (code === 1) {
        yield put({
          type: 'save',
          payload: data,
        });
        return Promise.resolve();
      }
      return Promise.reject(msg);
    },
    *sendAuthentication({ payload }, { call, put }) {
      const response = yield call(postAuthentication, payload);
      const { code, msg } = response;
      if (code === 1) {
        return Promise.resolve(msg);
      }
      return Promise.reject(msg);
    },
  },

  reducers: {
    save(state, { payload }) {
      state.info = payload;
      return state;
    },
  },
};
